package com.tech.connect.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ksy.common.utils.AndroidUtil;
import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.GridOffsetsItemDecoration;
import com.ksy.common.view.ProxyLayout;
import com.ksy.common.view.PullToRefreshLayout;
import com.tech.connect.R;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.MyHttp;
import com.tech.connect.model.ItemLvXin;
import com.tech.connect.model.PagerModel;
import com.tech.connect.zhaofuwu.ZhaoFuWuLvYouViewActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyGuanZhuActivity extends BaseActivity {

    private BaseAdapter<ItemLvXin, BaseHolder> adapter;
    private RecyclerView recycler;
    private List<ItemLvXin> allList = new ArrayList<>();
    private ProxyLayout<RecyclerView> proxyLayout;
    private int pageSize = 15;
    private int page = 1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_recycler);
        getHeadBar().setTitle("我的关注");
        initView();
        loadData();
    }

    private void initView() {
        recycler = findViewById(R.id.recycler);
        proxyLayout = new ProxyLayout<>(activity, recycler);
        proxyLayout.setCanRefresh(true);
        proxyLayout.setDragListener(new PullToRefreshLayout.DragListener() {
            @Override
            public void drag2Upper(PullToRefreshLayout pullToRefreshLayout) {
                ++page;
                loadData();
            }

            @Override
            public void drag2Downer(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                loadData();
            }
        });
        recycler.setLayoutManager(new GridLayoutManager(activity, 2));
        GridOffsetsItemDecoration itemDecoration = new GridOffsetsItemDecoration(GridOffsetsItemDecoration.GRID_OFFSETS_VERTICAL);
        itemDecoration.setHorizontalItemOffsets(AndroidUtil.dip2px(10));
        itemDecoration.setVerticalItemOffsets(AndroidUtil.dip2px(10));
        recycler.addItemDecoration(itemDecoration);
    }


    private void initAdapter(List<ItemLvXin> lvXinList) {
        if (lvXinList != null) {
            allList.addAll(lvXinList);
        }
        initAdapter();
    }

    private void initAdapter() {
        if (adapter == null) {
            adapter = new BaseAdapter<ItemLvXin, BaseHolder>(R.layout.item_layout_lvxing_pei, allList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ItemLvXin itemLvXin = allList.get(position);
                    TextView tvInfo = helper.getView(R.id.tvInfo);

                }
            };
            adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(activity, ZhaoFuWuLvYouViewActivity.class);
                    intent.putExtra("lvYouTitle", "苹果6s");
                    jump2Activity(intent);
                }
            });
            recycler.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
    }


    private void loadData() {
        Map<String, Object> map = new HashMap<>();
        map.put("pageNumber", page);
        map.put("pageSize", pageSize);
        MyHttp.myGuanZhu(map, new BaseEntityOb<PagerModel<ItemLvXin>>() {
            @Override
            public void onDataDeal(boolean isSuccess, PagerModel<ItemLvXin> data, String msg) {
                List<ItemLvXin> lvXinList = null;
                if (isSuccess && data != null) {
                    lvXinList = data.content;
                }
                if (page == 1) {
                    allList.clear();
                }
                initAdapter(lvXinList);
                if (lvXinList != null && lvXinList.size() >= pageSize) {
                    proxyLayout.setCanLoadMore(true);
                } else {
                    proxyLayout.setCanLoadMore(false);
                }
                if (allList.isEmpty()) {
                    proxyLayout.showEmptyView();
                } else {
                    proxyLayout.showContentView();
                }
                proxyLayout.dragFinish();
            }
        });
    }


}